package com.milo.coding.syncronized;

import ch.qos.logback.core.net.SyslogOutputStream;
import lombok.extern.slf4j.Slf4j;
import org.openjdk.jol.info.ClassLayout;


@Slf4j
public class T0_BasicLock {
    public static void main(String[] args) {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Object o = new Object();
        // 匿名偏向锁
        // 00000101 00000000 00000000 00000000
        log.info(ClassLayout.parseInstance(o).toPrintable());

        new Thread(()->{
            synchronized (o){
                // 偏向锁
                // 00000101 00110000 00100111 11100111
                log.info(ClassLayout.parseInstance(o).toPrintable());
            }
        }).start();

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 偏向锁
        // 00000101 00110000 00100111 1110011
        log.info(ClassLayout.parseInstance(o).toPrintable());
        new Thread(()->{
            // 升级为轻量级锁（俩线程交替执行）
            // 11101000 00001001 10001010 00001011
            synchronized (o){
                log.info(ClassLayout.parseInstance(o).toPrintable());
            }
        }).start();
    }
}
